﻿using ABPLHQCS.Departments;
using ABPLHQCS.Books ;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp;
using Volo.Abp.EntityFrameworkCore.Modeling;
using ABPLHQCS.FormInstances;
using ABPLHQCS.FormItems;
using ABPLHQCS.Categories;

namespace ABPLHQCS.EntityFrameworkCore
{
    public static class ABPConfigEFDbContext
    {
  
        public static void ConfigureEFDBContext(this ModelBuilder builder)
        {
            Check.NotNull(builder, "builder");

            builder.Entity(delegate (EntityTypeBuilder<FormItem> b)
            {
                b.ToTable(ABPLHQCSConsts.DbTablePrefix + "FormItem", ABPLHQCSConsts.DbSchema);
                b.ConfigureByConvention();
               // b.HasMany(e => e.FormInstances).WithOne(e=>e.FormItem);
                b.Property(x => x.FormName).IsRequired();
                b.HasOne<FormCategory>().WithMany().HasForeignKey(x => x.FormCategoryId).IsRequired();
            });

            builder.Entity(delegate (EntityTypeBuilder<FormCategory> b)
            {
                b.ToTable(ABPLHQCSConsts.DbTablePrefix + "FormCategory", ABPLHQCSConsts.DbSchema);
                b.ConfigureByConvention();
               // b.HasMany(x => x.FormItems).WithOne(x=>x.Category);
            });

            builder.Entity(delegate (EntityTypeBuilder<FormInstance> b)
            {
                b.ToTable(ABPLHQCSConsts.DbTablePrefix + "FormInstance", ABPLHQCSConsts.DbSchema);
                b.ConfigureByConvention();
               // b.HasOne<Department>().WithMany().HasForeignKey(x => x.DepartmentId).IsRequired();
                b.HasOne<FormItem>().WithMany().HasForeignKey(x => x.FormItemId).IsRequired();
              
            });

            builder.Entity(delegate (EntityTypeBuilder<Department> b)
            {
                b.ToTable(ABPLHQCSConsts.DbTablePrefix + "Department", ABPLHQCSConsts.DbSchema);
                b.ConfigureByConvention();
                b.HasMany(x => x.SubDepartment).WithOne(x => x.Parent);
            });



        }
    }
}
